package lq.auth.studyauth.configure;

import lq.auth.studyauth.service.StudyUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 用于处理/oauth开头的请求，
 * Spring Cloud OAuth内部定义的获取令牌，刷新令牌的请求地址都是以/oauth/开头的，
 * 也就是说FebsSecurityConfigure用于处理和令牌相关的请求
 *
 * 继承的WebSecurityConfigurerAdapter类中,使用了@Order(100),代表优先级为100
 * StudyResourceServerConfigure继承ResourceServerConfigurerAdapter使用了@EnableResourceServer注解
 * @EnableResourceServer 注解映入了ResourceServerConfiguration类(@Import({ResourceServerConfiguration.class}))
 * 在ResourceServerConfiguration中定义了优先级为3
 * 因为在本类过滤的是/oauth/**请求,而StudyResourceServerConfigure过滤所有请求,
 * 当在spring security中定于多个过滤器链的时候,根据其优先级,只有优先级高的进行匹配
 * 我们希望/oauth/**单独处理就要让WebSecurity优先级高
 * 所以使用@Order(2)将优先级定义为2
 * spring中数字越小,优先级越高
 */
@Order(2) // 定于优先级
@EnableWebSecurity // 开启和web相关的安全配置
public class StudySecurityConfigure extends WebSecurityConfigurerAdapter {
    @Autowired
    private StudyUserDetailService userDetailService;

    /**
     * 因为在启动类使用了@StudyCloudApplication注解
     * 该注解引入了StudyServerProtectConfigure配置
     * StudyServerProtectConfigure配置将PasswordEncoder添加到了IOC容器中
     * 因此只要使用了@StudyCloudApplication注解的都可以直接注入PasswordEncoder
     */
    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 密码模式需要用到这个bean
     * @return
     * @throws Exception
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    /**
     * requestMatchers().antMatchers("/oauth/**")的含义是：FebsSecurityConfigure安全配置类只对/oauth/开头的请求有效。
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatchers()
                .antMatchers("/oauth/**")
                .and()
                .authorizeRequests()
                .antMatchers("/oauth/**").authenticated()
                .and()
                .csrf().disable();
    }

    /**
     * 指定了userDetailsService和passwordEncoder。
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailService).passwordEncoder(passwordEncoder);
    }

    /**
     * 用于加密和加密校验的方法
     * PasswordEncoder 对于同一个字符串密码，加密后的密文不一样
     * (现在提到Common下StudyServerProtectConfigure注入IOC)
     * @return
     */
  /* @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
*/
   /* public static void main(String[] args) {
        String password = "123456";
        PasswordEncoder encoder = new BCryptPasswordEncoder();
        System.out.println(encoder.encode(password));
        System.out.println(encoder.encode(password));
    }*/
}
